package com.bto.solar.work.controller;

import com.bto.solar.work.aspect.RequestLog;
import com.bto.solar.work.common.R;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.dto.QueryCusEarningsReq;
import com.bto.solar.work.dto.UpdateOrderStateReq;
import com.bto.solar.work.dto.order.ConstructDto;
import com.bto.solar.work.dto.order.CreateOrderReq;
import com.bto.solar.work.dto.order.CustomerInfoDto;
import com.bto.solar.work.dto.order.DesignDto;
import com.bto.solar.work.dto.order.DeviceDto;
import com.bto.solar.work.dto.order.GSContractDto;
import com.bto.solar.work.dto.order.OrderGridDto;
import com.bto.solar.work.dto.order.PreApprovalDto;
import com.bto.solar.work.dto.order.QueryOrderReq;
import com.bto.solar.work.dto.order.QueryOrderRes;
import com.bto.solar.work.dto.order.RecordDto;
import com.bto.solar.work.dto.order.SettlementDto;
import com.bto.solar.work.dto.order.TaKanDto;
import com.bto.solar.work.dto.order.TecKanChaDto;
import com.bto.solar.work.entity.Construct;
import com.bto.solar.work.entity.GridConnection;
import com.bto.solar.work.entity.Settlement;
import com.bto.solar.work.service.BtoOrderService;
import com.bto.solar.work.service.BtoOrderStateService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@RequestLog(description = "OrderController")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private BtoOrderService btoOrderService;




    @ApiModelProperty("创建订单")
    @PostMapping("create")
    public R<String> createOrder(@RequestParam("type") String type , @RequestBody CreateOrderReq createOrderReq){
        return R.ok(btoOrderService.createNewOrder(createOrderReq).getOrderId());
    }

    /**---------------------------------预审信息-------------------------------**/

    @ApiModelProperty("提交预审信息")
    @PostMapping("put-pre-approval")
    public R putPreApproval(@RequestBody PreApprovalDto req){
        return R.ok(btoOrderService.putOrderPreApproval(req));
    }


    @ApiModelProperty("查询预审信息")
    @GetMapping("get-pre-approval")
    public R<PreApprovalDto> getPreApproval(String orderId){
        return R.ok(btoOrderService.getOrderPreApproval(orderId));
    }


    /**---------------------------------业主信息-------------------------------**/
    @ApiModelProperty("提交-客户信息")
    @PostMapping("put-customer-info")
    public R putCustomerInfo(@RequestBody CustomerInfoDto customerInfoDto){
        btoOrderService.putCustomerInfo(customerInfoDto);
        return R.ok();
    }

    @ApiModelProperty("查询-客户信息")
    @GetMapping("get-customer-info")
    public R<CustomerInfoDto> getCustomerInfo(String orderId){
        return R.ok(btoOrderService.getCustomerInfo(orderId));
    }

    /**---------------------------------踏勘信息-------------------------------**/
    @ApiModelProperty("提交踏勘信息")
    @PostMapping("put-takan")
    public R putTaKan(@RequestBody TaKanDto taKanDto){

        btoOrderService.putOrderTaKan(taKanDto);
        return R.ok();
    }

    @ApiModelProperty("查询踏勘信息")
    @GetMapping("get-takan")
    public R<TaKanDto> getTaKan(String orderId){
        return R.ok(btoOrderService.getOrderTaKan(orderId));
    }

    /**---------------------------------技术勘察信息-------------------------------**/
    @ApiModelProperty("提交-技术勘察信息")
    @PostMapping("put-tec-kancha")
    public R putTecKanCha(@RequestBody TecKanChaDto tecKanChaDto){
        btoOrderService.putOrderTecKanCha(tecKanChaDto);
        return R.ok();
    }
    @ApiModelProperty("查询技术勘察信息")
    @GetMapping("get-tec-kancha")
    public R<TecKanChaDto> getTecKanCha(String  orderId){
        return R.ok(btoOrderService.getOrderTecKanCha(orderId));
    }

    /**---------------------------------签约合同-------------------------------**/
    @ApiModelProperty("查询订单签约合同")
    @GetMapping("get-lease-contract")
    public R getContract(String orderId){
        return R.ok(btoOrderService.getOrderLeaseContractSign(orderId));
    }

    /**---------------------------------设计信息-------------------------------**/
    @ApiModelProperty("提交订单设计信息")
    @PostMapping("put-design")
    public R putDesign(@RequestBody DesignDto designDto){
        btoOrderService.putOrderDesign(designDto);
        return R.ok();
    }
    @ApiModelProperty("查询订单设计信息")
    @GetMapping("get-design")
    public R<DesignDto> getDesign(@RequestParam String orderId , @RequestParam(required = true) Integer times){
        return R.ok(btoOrderService.getOrderDesign(orderId , times));
    }

    /**---------------------------------施工-施工信息-------------------------------**/
    @ApiModelProperty("提交订单施工信息")
    @PostMapping("put-construction")
    public R putConstruction(@RequestBody Construct construct){
        btoOrderService.putOrderConstruct(construct);
        return R.ok();
    }
    @ApiModelProperty("查询订单施工信息")
    @GetMapping("get-construction")
    public R<ConstructDto> getConstruction(String orderId){
        return R.ok(btoOrderService.getOrderConstruct(orderId));
    }


    /**---------------------------------施工-影像件信息-------------------------------**/
    @ApiModelProperty("提交订单施工影像信息")
    @PostMapping("put-construction-images")
    public R putConstructionImages(@RequestBody ConstructDto constructDto){
        btoOrderService.putOrderConstructImages(constructDto);
        return R.ok();
    }
    @ApiModelProperty("查询订单施工影像信息")
    @GetMapping("get-construction-images")
    public R<ConstructDto> getConstructionImage(String orderId){
        return R.ok(btoOrderService.getOrderConstructImages(orderId));
    }

    /**---------------------------------施工-设备信息-------------------------------**/
    @ApiModelProperty("提交订单设备信息")
    @PostMapping("put-device")
    public R putDevice(@RequestBody DeviceDto device){
        btoOrderService.putOrderConstructDevice(device);
        return R.ok();
    }
    @ApiModelProperty("提交订单设备扫码信息")
    @PostMapping("put-device-scan")
    public R putDeviceScan(@RequestBody DeviceDto deviceDto){
        btoOrderService.putOrderConstructDeviceScan(deviceDto);
        return R.ok();
    }
    @ApiModelProperty("查询订单设备信息")
    @GetMapping("get-device")
    public R<DeviceDto> getDevice(String orderId , String deviceType){
        return R.ok(btoOrderService.getOrderConstructDevice(orderId , deviceType));
    }


    /**---------------------------------备案证信息-------------------------------**/
    @ApiModelProperty("提交订单备案信息")
    @PostMapping("put-record")
    public R putRecord(@RequestBody RecordDto recordDto){
        btoOrderService.putOrderRecord(recordDto);
        return R.ok();
    }
    @ApiModelProperty("查询订单备案信息")
    @GetMapping("get-record")
    public R getRecord(String orderId){
        return R.ok(btoOrderService.getOrderRecord(orderId));
    }


    /**---------------------------------并网信息-------------------------------**/
    @ApiModelProperty("提交订单并网信息")
    @PostMapping("put-grid")
    public R putGrid(@RequestBody OrderGridDto gridDto){
        btoOrderService.putOrderGrid(gridDto);
        return R.ok();
    }
    @ApiModelProperty("查询订单并网信息")
    @GetMapping("get-grid")
    public R<OrderGridDto> getGrid(String orderId){
        return R.ok(btoOrderService.getOrderGrid(orderId));
    }

    /**---------------------------------结算信息：直营结算、代理商：并网结算、资料结算-------------------------------**/
    @ApiModelProperty("提交订单结算信息")
    @PostMapping("put-grid-settlement")
    public R putGridSettlement(@RequestBody Settlement settlement){
        btoOrderService.putOrderGridSettlement(settlement);
        return R.ok();
    }
    @ApiModelProperty("查询订单-结算信息")
    @GetMapping("get-grid-settlement")
    public R<SettlementDto> getGridSettlement(String orderId , Integer type){
        Settlement query = new Settlement();
        query.setOrderId(orderId);
        query.setSettlementType(type.byteValue());
        return R.ok(btoOrderService.getOrderGridSettlement(query));
    }


    /**---------------------------------购售电合同信息-------------------------------**/
    @ApiModelProperty("提交订单购售电合同信息")
    @PostMapping("put-gs-elec-contract")
    public R putGSElecContractMeans(@RequestBody GridConnection gridConnection){
        btoOrderService.putOrderGSContract(gridConnection);
        return R.ok();
    }

    @ApiModelProperty("查询订单购售电合同信息")
    @GetMapping("get-gs-elec-contract")
    public R<GSContractDto> getGSElecContractMeans(String orderId){
        return R.ok(btoOrderService.getOrderGSContract(orderId));
    }



    @ApiModelProperty("查询订单客户收益")
    @PostMapping("get-customer-earnings")
    public R getCustomerEarnings(@RequestBody QueryCusEarningsReq cusEarningsReq){
        return R.ok(btoOrderService.queryCustomerEarnings(cusEarningsReq));
    }

    @ApiModelProperty("订单查询")
    @PostMapping("search")
    public R<PageInfo<QueryOrderRes>> search(@RequestParam(required = false , defaultValue = "1") Integer pageNum ,
                                             @RequestParam(required = false , defaultValue = "20") Integer pageSize ,
                                            @RequestBody(required = false) QueryOrderReq orderReq){
        orderReq.setSortByState(false);
        return R.ok(btoOrderService.getOrderList(orderReq , pageNum , pageSize));
    }

    @ApiModelProperty("订单查询-排除特殊")
    @PostMapping("search-exclude-states")
    public R<PageInfo<QueryOrderRes>> searchExcludeSpecial(@RequestParam(required = false , defaultValue = "1") Integer pageNum ,
                                             @RequestParam(required = false , defaultValue = "20") Integer pageSize ,
                                             @RequestBody QueryOrderReq orderReq){
        List<String> excludeSpecialStates = new ArrayList<>(Constants.SPECIAL_STATES);
        //如果不是查询PASS ， 就把PASS 的去掉
        List<String> excludeTaskStates = new ArrayList<>();
        orderReq.setSortByState(true);
        orderReq.setExcludeTaskStateIds(excludeTaskStates);
        orderReq.setExcludeSpecialStateIds(excludeSpecialStates);
        return R.ok(btoOrderService.getOrderList(orderReq , pageNum , pageSize));
    }

    @ApiModelProperty("订单审批列表（待审批-已审批）- 网页端 - 弃用")
    @PostMapping("approval-list")
    public R<PageInfo<QueryOrderRes>> orderApprovalList(@RequestParam(required = false , defaultValue = "1") Integer pageNum ,
                                             @RequestParam(required = false , defaultValue = "20") Integer pageSize ,
                                             @RequestBody QueryOrderReq orderReq){
        return R.ok(btoOrderService.getOrderApprovalList(orderReq , pageNum , pageSize));
    }


    @ApiModelProperty("订单状态更新")
    @PostMapping("/state/update")
    public R stateUpdate(UpdateOrderStateReq updateOrderStateReq){
        String orderId = updateOrderStateReq.getOrderId();
        String stageId = updateOrderStateReq.getStageId();
        String taskId = updateOrderStateReq.getTaskId();
        String preStateId = updateOrderStateReq.getPreStateId();
        String targetStateId = updateOrderStateReq.getTargetStateId();
        if(BizTaskStateEnum.LOCK.getStateId().equals(targetStateId)){
            btoOrderStateService.updateOrSaveOrderState(orderId , null , null , targetStateId , null , false);
        }
        else if(BizTaskStateEnum.UN_LOCK.getStateId().equals(targetStateId)){
            btoOrderStateService.updateOrSaveOrderState(orderId , null , null , targetStateId , BizTaskStateEnum.LOCK.getStateId() , true);
        }
        else {
            btoOrderStateService.updateOrSaveOrderState(orderId , stageId , taskId , targetStateId , preStateId , true);
        }
        return R.ok();
    }

}
